package org.jeecg.modules.demo.baseordercommodity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.baseorde.entity.BaseOrder;
import org.jeecg.modules.demo.baseorde.service.IBaseOrderService;
import org.jeecg.modules.demo.baseordercommodity.entity.*;
import org.jeecg.modules.demo.baseordercommodity.service.IBaseOrderCommodityService;
import org.jeecg.modules.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 订单商品表
 * @Author: jeecg-boot
 * @Date: 2022-02-11
 * @Version: V1.0
 */
@Api(tags = "订单商品表")
@RestController
@RequestMapping("/baseordercommodity/baseOrderCommodity")
@Slf4j
public class BaseOrderCommodityController extends JeecgController<BaseOrderCommodity, IBaseOrderCommodityService> {
    @Autowired
    private IBaseOrderCommodityService baseOrderCommodityService;
    @Autowired
    private IBaseOrderService baseOrderervice;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 分页列表查询
     *
     * @param baseOrderCommodity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "订单商品表-分页列表查询")
    @ApiOperation(value = "订单商品表-分页列表查询", notes = "订单商品表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BaseOrderCommodity baseOrderCommodity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BaseOrderCommodity> queryWrapper = QueryGenerator.initQueryWrapper(baseOrderCommodity, req.getParameterMap());
        Page<BaseOrderCommodity> page = new Page<BaseOrderCommodity>(pageNo, pageSize);
        IPage<BaseOrderCommodity> pageList = baseOrderCommodityService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据订单id查询
     *
     * @param orderId
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "根据订单id查询")
    @ApiOperation(value = "订根据订单id查询", notes = "根据订单id查询")
    @GetMapping(value = "/getByOrderId")
    public Result<?> getByOrderId(@RequestParam(name = "orderId", defaultValue = "") String orderId,
                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                  HttpServletRequest req) {
        Page<BaseOrderCommodity> page = new Page<BaseOrderCommodity>(pageNo, pageSize);
        IPage<BaseOrderCommodity> pageList = baseOrderCommodityService.getByOderId(page, orderId);
        return Result.OK(pageList);
    }

    /**
     * 查询线上自提订单
     *
     * @param memberName
     * @param orderCode
     * @param beginTime
     * @param endTime
     * @param memberPhone
     * @param orderStatus
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "查询线上自提订单")
    @ApiOperation(value = "查询线上自提订单", notes = "查询线上自提订单")
    @GetMapping(value = "/searchSelfmentionOrder")
    public Result<?> searchSelfmentionOrder(@RequestParam(name = "memberName", defaultValue = "") String memberName,
                                            @RequestParam(name = "orderCode", defaultValue = "") String orderCode,
                                            @RequestParam(name = "beginTime", defaultValue = "") String beginTime,
                                            @RequestParam(name = "endTime", defaultValue = "") String endTime,
                                            @RequestParam(name = "memberPhone", defaultValue = "") String memberPhone,
                                            @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus,
                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                            HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }
        List<BaseOrder> listOrder = baseOrderervice.searchOnlineOrderList(memberName, orderCode, beginTime, endTime, memberPhone, orderStatus, storeId);
        List<SelfMetionOrder> listSelfMetionOrder = new ArrayList<SelfMetionOrder>();
        for (BaseOrder order : listOrder) {
            List<SelfMentionCommodity> listSelfMentionCommodity = baseOrderCommodityService.selectSelfMentionCommodityByOrderId(order.getId());
            SelfMetionOrder selfMetionOrder = new SelfMetionOrder();
            selfMetionOrder.setOrderId(order.getId());
            selfMetionOrder.setOrderCode(order.getOrderCode());
            selfMetionOrder.setPayCode(order.getPayCode());
            selfMetionOrder.setStatus(order.getOrderStatus());
            selfMetionOrder.setListSelfMentionCommodity(listSelfMentionCommodity);
            listSelfMetionOrder.add(selfMetionOrder);
        }
        return Result.OK(listSelfMetionOrder);
    }

    /**
     * 查询线上订单商品列表
     *
     * @param pageNo
     * @param storeId
     * @param memberId
     * @param orderStatus
     * @return
     */
    @AutoLog(value = "查询线上订单商品列表")
    @ApiOperation(value = "查询线上订单商品列表", notes = "查询线上订单商品列表")
    @GetMapping(value = "/getOnlineOrderCommodityList")
    public Result<List<CommodityMain>> getOnlineOrderCommodityList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                   @RequestParam(name = "storeId", defaultValue = "") String storeId,
                                                                   @RequestParam(name = "memberId", defaultValue = "") String memberId,
                                                                   @RequestParam(name = "orderStatus", defaultValue = "") String orderStatus) {
        Result<List<CommodityMain>> result = new Result<>();
        List<CommodityMain> list = baseOrderCommodityService.getOnlineOrderCommodityList(pageNo, 10, storeId, memberId, orderStatus);
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

    /**
     * 根据订单id查询商品列表
     *
     * @param pageNo
     * @param orderId
     * @return
     */
    @AutoLog(value = "根据订单id查询商品列表")
    @ApiOperation(value = "根据订单id查询商品列表", notes = "根据订单id查询商品列表")
    @GetMapping(value = "/getCommodityInfoListByOrderId")
    public Result<List<OrderCommodityInfo>> getCommodityInfoListByOrderId(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                          @RequestParam(name = "orderId", defaultValue = "") String orderId) {
        Result<List<OrderCommodityInfo>> result = new Result<>();
        List<OrderCommodityInfo> list = baseOrderCommodityService.getCommodityInfoListByOrderId(pageNo, 10, orderId);
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

    /**
     * 添加
     *
     * @param baseOrderCommodity
     * @return
     */
    @AutoLog(value = "订单商品表-添加")
    @ApiOperation(value = "订单商品表-添加", notes = "订单商品表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BaseOrderCommodity baseOrderCommodity) {
        baseOrderCommodityService.save(baseOrderCommodity);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param baseOrderCommodity
     * @return
     */
    @AutoLog(value = "订单商品表-编辑")
    @ApiOperation(value = "订单商品表-编辑", notes = "订单商品表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BaseOrderCommodity baseOrderCommodity) {
        baseOrderCommodityService.updateById(baseOrderCommodity);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "订单商品表-通过id删除")
    @ApiOperation(value = "订单商品表-通过id删除", notes = "订单商品表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        baseOrderCommodityService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "订单商品表-批量删除")
    @ApiOperation(value = "订单商品表-批量删除", notes = "订单商品表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.baseOrderCommodityService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "订单商品表-通过id查询")
    @ApiOperation(value = "订单商品表-通过id查询", notes = "订单商品表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BaseOrderCommodity baseOrderCommodity = baseOrderCommodityService.getById(id);
        if (baseOrderCommodity == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(baseOrderCommodity);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param baseOrderCommodity
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BaseOrderCommodity baseOrderCommodity) {
        return super.exportXls(request, baseOrderCommodity, BaseOrderCommodity.class, "订单商品表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BaseOrderCommodity.class);
    }

}
